﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CustomerEmailer.Common;
using System.Runtime.InteropServices;

namespace CustomerEmailer
{
    public partial class AttachFile : Form
    {
        public const int WM_NCLBUTTONDOWN = 0xA1;
        public const int HT_CAPTION = 0x2;

        [DllImportAttribute("user32.dll")]
        public static extern int SendMessage(IntPtr hWnd,
                         int Msg, int wParam, int lParam);
        [DllImportAttribute("user32.dll")]
        public static extern bool ReleaseCapture();
        Main ParentForm { get; set; }
        public AttachFile(Main parentForm)
        {
            ParentForm = parentForm;
            InitializeComponent();
        }

        private void AttachFile_Load(object sender, EventArgs e)
        {
            lstSelectedFiles.Items.Clear();
            foreach (var attachedFile in ParentForm.AttachedFiles)
            {
                lstSelectedFiles.Items.Add(attachedFile.ToString());
            }
        }

        private void btnBrowseFile_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
        }

        private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
        {
            //tbFilePath.Text = openFileDialog1.FileName;
            if (openFileDialog1.FileName != "" && openFileDialog1.FileName != "openFileDialog1")
            {
                lstSelectedFiles.Items.Add(openFileDialog1.FileName);
            }
        }

        private void btnRemoveAttachment_Click(object sender, EventArgs e)
        {
            if (lstSelectedFiles.SelectedIndex > -1)
            {
                lstSelectedFiles.Items.Remove(lstSelectedFiles.SelectedItem);
            }
        }

        private void btnComplete_Click(object sender, EventArgs e)
        {
            ParentForm.AttachedFiles.Clear();
            foreach (var item in lstSelectedFiles.Items)
            {
                ParentForm.AttachedFiles.Add(item.ToString());
            }

            this.Close();
        }

        private void AttachFile_Paint(object sender, PaintEventArgs e)
        {
            this.Region = new System.Drawing.Region(Utils.drawRoundedRect(0, 0, 15, this.Height, this.Width));
            Utils.drawBorder(0, 0, 15, this.Height, this.Width, e);
        }

        private void AttachFile_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                ReleaseCapture();
                SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
            }
        }
    }
}
